/**
 * Created by hcadmin on 16/4/18.
 */
define([
    "jquery",
    "IO",
    "UPLOADUI",
    "UPLOAD",
    "jqueryUI",
    "bootbox"
], function ($, IO, upui, up, jui, bootbox, definebt) {
    var question_count=0;
    var paper_id;
    $(function(){
        //初始化
        paper_id=$('.ct').attr('paper_id');
        //加载处理
        $('body').css("overflow","hidden");
        $("#cover").show();
        show();
        //添加判断题
        $("#judgement").click(function(){
            judgement();
            $(".judgement_mian:last .text").focus();
            $(".ct").animate({ scrollTop: $('.ct')[0].scrollHeight}, 1000);
        });

        //添加单选题
        $("#single").click(function(){
            single();
            $(".single_main:last .text:first").focus();
            $(".ct").animate({ scrollTop: $('.ct')[0].scrollHeight}, 1000);
        });

        //添加多选题
        $("#multiple").click(function(){
            multiple();
            $(".multiple_main:last .text:first").focus();
            $(".ct").animate({ scrollTop: $('.ct')[0].scrollHeight}, 1000);
        });
        
        //添加问答题
        $("#subjective").click(function(){
            subjective();
            $(".subjective_main:last .text").focus();
            $(".ct").animate({ scrollTop: $('.ct')[0].scrollHeight}, 1000);
        });

        //删除题目
        $(document).on('click','.deleteDiv',function(){
            var self=$(this).parent().parent().parent();
            var count=self.nextAll().length;
            var next= self.nextAll().first();
            for(var i=0;i<count;i++){
                next.find('.question_count').text(next.find('.question_count').text()-1);
                next=next.next();
            }
            if(self.attr('question_id')>0){
                self.css('display','none');
                self.attr('del_flag','1');

            }else{
                self.remove();
            }

            question_count-=1;
        });

       //单选题添加选项
        $(document).on('click','.add1',function(){
            $(this).parents('.single_main').find('.list1').append(
                '<div class="row list1_div" style="margin-bottom: 20px;">'+
                '<div class="col-md-3" style="text-align: right;padding-top: 5px;">'+
                '<span>'+
                '选项<span class="span1">1</span>.'+
                '</span>'+
                '</div>'+
                '<div class="col-md-6">'+
                '<input type="text" class="option" style="height: 34px;width:100%; border: 1px solid grey;" />'+
                '</div>'+
                '<div class="col-md-3">'+
                '<a class="delete1" href="#" style="color: red;padding-top: 5px;display: inline-block;">'+
                '删除选项'+
                '</a>'+
                '</div>'+
                '</div>'
            )
            $(this).parents('.single_main').find('.span1:last').text( $(this).parents('.single_main').find('.list1').children().length)

        });
        //多选题添加选项
        $(document).on('click','.add2',function(){
            $(this).parents('.multiple_main').find('.list2').append(
                '<div class="row list2_div" style="margin-bottom: 20px;">'+
                '<div class="col-md-3" style="text-align: right;padding-top: 5px;">'+
                '<span>'+
                '选项<span class="span2">1</span>.'+
                '</span>'+
                '</div>'+
                '<div class="col-md-6">'+
                '<input type="text" class="option" style="height: 34px;width:100%; border: 1px solid grey;" />'+
                '</div>'+
                '<div class="col-md-3">'+
                '<a class="delete2" href="#" style="color: red;padding-top: 5px;display: inline-block;">'+
                '删除选项'+
                '</a>'+
                '</div>'+
                '</div>');
            $(this).parents('.multiple_main').find('.span2:last').text( $(this).parents('.multiple_main').find('.list2').children().length)


        });


        //删除单选选项
        $(document).on('click','.delete1',function(){
             var self=$(this).parents('.list1_div');
             var count=self.nextAll().length;
             var next=self.nextAll().first();
             for(var i=0;i<count;i++){
                 next.find('.span1').text(next.find('.span1').text()-1);
                 next=next.next();
             }
             $(this).parents('.list1_div').remove();
        });
        //删除多选选项
        $(document).on('click','.delete2',function(){
            var self=$(this).parents('.list2_div');
            var count=self.nextAll().length;
            var next=self.nextAll().first();
            for(var i=0;i<count;i++){
                next.find('.span2').text(next.find('.span2').text()-1);
                next=next.next();
            }
            $(this).parents('.list2_div').remove();

        });
       //点击发布按钮
        $('.btnSubmit').on('click',function () {
            var dataAll=$('.ct').children();
            var count=dataAll.length;
            if(count<=0){
                bootbox.alert('请添加题目');
                return;
            }
            var childFirst=dataAll.first();
            var question_list="[";
            for(var i=0;i<count;i++){
                var question_type= childFirst.attr ('question_type');//题目类型
                var question_id=childFirst.attr ('question_id');//题目id
                var del_flag=childFirst.attr ('del_flag'); //题目删除标识
                var question_body;
                var question_options="";
                switch (question_type) {
                    case '0':  //单选题
                        question_body=childFirst.find('.text').val();
                        if(question_body==''&&del_flag==0){
                            bootbox.alert("单选题"+(i+1)+"问题为空");
                            return;
                        }
                        var optionAll=childFirst.find('.list1').children(); //所有选项
                        var optionCount=optionAll.length; //所有选项
                        var optionFirst=optionAll.first();//第一个选项
                        var arry=[];
                        for(var j=0;j<optionCount;j++){
                            var option= optionFirst.find('.option').val();
                            if(option==''&&del_flag==0){
                                bootbox.alert("单选题"+(i+1)+" 选项"+(j+1)+"为空");
                                return;
                            }

                            arry.push(option)
                            optionFirst=optionFirst.next();
                        }
                        question_options=arry.join('^');
                        break;
                    case '1':  //判断题
                        question_body=childFirst.find('.text').val();
                        if(question_body==''&&del_flag==0){
                            bootbox.alert("判断题"+(i+1)+"问题为空");
                            return;
                        }
                        question_options="";
                        break;
                    case '2':  //多选题
                        question_body=childFirst.find('.text').val();
                        if(question_body==''&&del_flag==0){
                            bootbox.alert("多选题"+(i+1)+"问题为空");
                            return;
                        }
                        var optionAll=childFirst.find('.list2').children(); //所有选项
                        var optionCount=optionAll.length; //所有选项
                        var optionFirst=optionAll.first();//第一个选项
                        var arry=[];
                        for(var j=0;j<optionCount;j++){
                            var option= optionFirst.find('.option').val();
                            if(option==''&&del_flag==0){
                                bootbox.alert("多选题"+(i+1)+" 选项"+(j+1)+"为空");
                                return;
                            }
                            arry.push(option)
                            optionFirst=optionFirst.next();
                        }
                        question_options=arry.join('^');
                        break;
                    case '3':  //问答题
                        question_body=childFirst.find('.text').val();
                        if(option==''&&del_flag==0){
                            bootbox.alert("问答题"+(i+1)+"问题为空");
                            return;
                        }
                        question_options="";
                        break;
                    default:

                }

                question_list+=",{\"question_id\":\""+question_id+"\"," +
                    "\"question_body\":\""+question_body+"\"," +
                    "\"question_options\":\""+question_options+"\"," +
                    "\"question_type\":\""+question_type+"\"," +
                    "\"del_flag\":\""+del_flag+"\"}"
                childFirst=childFirst.next();
            }
            question_list+="]";
            question_list=question_list.replace(',','');
            var  param={};
            param.paper_id=paper_id;//课程id
            param.question_status=1;//发布
            param.question_list=question_list;
            IO.get('/questionsAdd',param,function (d) {
                if(d.code==0){
                    bootbox.alert(d.msg,function () {
                        $('.ct').empty();
                        question_count=0;
                        show();
                    });
                }else{
                    bootbox.alert(d.msg);
                }
            })


        })

        //点击保存按钮
        $('.btnSure').on('click',function () {
            var dataAll=$('.ct').children();
            var count=dataAll.length;
            if(count<=0){
                bootbox.alert('请添加题目');
                return;
            }
            var childFirst=dataAll.first();
            var question_list="[";
            for(var i=0;i<count;i++){
                var question_type= childFirst.attr ('question_type');//题目类型
                var question_id=childFirst.attr ('question_id');//题目id
                var del_flag=childFirst.attr ('del_flag'); //题目删除标识
                var question_body;
                var question_options="";
                switch (question_type) {
                    case '0':  //单选题
                        question_body=childFirst.find('.text').val();
                        if(question_body==''&&del_flag==0){
                            bootbox.alert("单选题"+(i+1)+"标题为空");
                            return;
                        }
                        var optionAll=childFirst.find('.list1').children(); //所有选项
                        var optionCount=optionAll.length; //所有选项
                        var optionFirst=optionAll.first();//第一个选项
                        var arry=[];
                        for(var j=0;j<optionCount;j++){
                            var option= optionFirst.find('.option').val();
                            if(option==''&&del_flag==0){
                                bootbox.alert("单选题"+(i+1)+" 选项"+(j+1)+"为空");
                                return;
                            }

                            arry.push(option)
                            optionFirst=optionFirst.next();
                        }
                        question_options=arry.join('^');
                        break;
                    case '1':  //判断题
                        question_body=childFirst.find('.text').val();
                        if(question_body==''&&del_flag==0){
                            bootbox.alert("判断题"+(i+1)+"标题为空");
                            return;
                        }
                        question_options="";
                        break;
                    case '2':  //多选题
                        question_body=childFirst.find('.text').val();
                        if(question_body==''&&del_flag==0){
                            bootbox.alert("多选题"+(i+1)+"标题为空");
                            return;
                        }
                        var optionAll=childFirst.find('.list2').children(); //所有选项
                        var optionCount=optionAll.length; //所有选项
                        var optionFirst=optionAll.first();//第一个选项
                        var arry=[];
                        for(var j=0;j<optionCount;j++){
                            var option= optionFirst.find('.option').val();
                            if(option==''&&del_flag==0){
                                bootbox.alert("多选题"+(i+1)+" 选项"+(j+1)+"为空");
                                return;
                            }
                            arry.push(option)
                            optionFirst=optionFirst.next();
                        }
                        question_options=arry.join('^');
                        break;
                    case '3':  //问答题
                        question_body=childFirst.find('.text').val();
                        if(option==''&&del_flag==0){
                            bootbox.alert("问答题"+(i+1)+"标题为空");
                            return;
                        }
                        question_options="";
                        break;
                    default:

                }

                question_list+=",{\"question_id\":\""+question_id+"\"," +
                    "\"question_body\":\""+question_body+"\"," +
                    "\"question_options\":\""+question_options+"\"," +
                    "\"question_type\":\""+question_type+"\"," +
                    "\"del_flag\":\""+del_flag+"\"}"
                childFirst=childFirst.next();
            }
            question_list+="]";
            question_list=question_list.replace(',','');
            var  param={};
            param.paper_id=paper_id;//课程id
            param.question_status=0;//保存
            param.question_list=question_list;
            IO.get('/questionsAdd',param,function (d) {
                if(d.code==0){
                    bootbox.alert(d.msg,function () {
                        $('.ct').empty();
                        question_count=0;
                        show();
                    });
                }else{
                    bootbox.alert(d.msg);
                }
            })

        })
        

        $(document).on('mouseleave','.main',function(event){
            $(this).css('border','1px solid lightgrey')
        });
        $(document).on('mouseenter','.main',function(event){
            $(this).css('border','2px solid #6ab7ad')

        });

        $(document).on('mouseleave','.deleteDiv',function(event){
            $(this).css('background-color','rgb(0,0,0,0)')
        });
        $(document).on('mouseenter','.deleteDiv',function(event){
            $(this).css('background-color','gainsboro')

        });

        //试卷初始化
        function show() {
            var  dataAll; //获得所有数据
            var  count;//题目个数;
            var  question_type;//题目类型
            var param={};
            param.paper_id=paper_id;
            param.listType=0;
            IO.get('/questionsList',param,function (d) {
                if(d.code==0){
                    //加载处理
                    $('body').css("overflow","hidden");
                    $("#cover").hide();
                    dataAll=d.data;
                    count=dataAll.length;
                    for(var i=0;i<count;i++) {
                        question_type = dataAll[i].question_type;
                        switch (question_type) {
                            case 0:  //单选题
                                single();
                                $('.ct').children().last().find('.text').val(dataAll[i].question_body);
                                var optionAll=dataAll[i].question_options.split('^');
                                var optionCount=optionAll.length;
                                var optionFirst=$('.ct').children().last().find('.list1').children().first();
                                for(var j=0;j<optionCount;j++){
                                    if(j>1){
                                        $('.ct').children().last().find('.list1').append(  '<div class="row list1_div" style="margin-bottom: 20px;">'+
                                            '<div class="col-md-3" style="text-align: right;padding-top: 5px;">'+
                                            '<span>'+
                                            '选项<span class="span1">1</span>.'+
                                            '</span>'+
                                            '</div>'+
                                            '<div class="col-md-6">'+
                                            '<input type="text" class="option" style="height: 34px;width:100%; border: 1px solid grey;" />'+
                                            '</div>'+
                                            '<div class="col-md-3">'+
                                            '<a class="delete1" href="#" style="color: red;padding-top: 5px;display: inline-block;">'+
                                            '删除选项'+
                                            '</a>'+
                                            '</div>'+
                                            '</div>');
                                        // $('.ct').children().last().find('.span1:last').text( $('.ct').children().last().find('.list1').children().length);
                                        optionFirst=optionFirst.next();
                                        optionFirst.find('.option').val(optionAll[j]);
                                        optionFirst.find('.span1:last').text(optionFirst.parent().children().length)
                                    }else if(j==0){
                                        optionFirst.find('.option').val(optionAll[j]);
                                        optionFirst=optionFirst.next();
                                    }else if(j==1){
                                        optionFirst.find('.option').val(optionAll[j]);
                                    }
                                }
                                $('.ct').children().last().attr('question_id',dataAll[i].question_id);
                                break;
                            case 1:  //判断题
                                judgement();
                                $('.ct').children().last().find('.text').val(dataAll[i].question_body);
                                $('.ct').children().last().attr('question_id',dataAll[i].question_id);
                                break;
                            case 2:  //多选题
                                multiple();
                                $('.ct').children().last().find('.text').val(dataAll[i].question_body);
                                var optionAll=dataAll[i].question_options.split('^');
                                var optionCount=optionAll.length;
                                var optionFirst=$('.ct').children().last().find('.list2').children().first();
                                for(var j=0;j<optionCount;j++){
                                    if(j>1){
                                        $('.ct').children().last().find('.list2').append(  '<div class="row list2_div" style="margin-bottom: 20px;">'+
                                            '<div class="col-md-3" style="text-align: right;padding-top: 5px;">'+
                                            '<span>'+
                                            '选项<span class="span2">1</span>.'+
                                            '</span>'+
                                            '</div>'+
                                            '<div class="col-md-6">'+
                                            '<input type="text" class="option" style="height: 34px;width:100%; border: 1px solid grey;" />'+
                                            '</div>'+
                                            '<div class="col-md-3">'+
                                            '<a class="delete2" href="#" style="color: red;padding-top: 5px;display: inline-block;">'+
                                            '删除选项'+
                                            '</a>'+
                                            '</div>'+
                                            '</div>');
                                        optionFirst=optionFirst.next();
                                        optionFirst.find('.option').val(optionAll[j]);
                                        optionFirst.find('.span2:last').text(optionFirst.parent().children().length)
                                    }else if(j==0){
                                        optionFirst.find('.option').val(optionAll[j]);
                                        optionFirst=optionFirst.next();
                                    }else if(j==1){
                                        optionFirst.find('.option').val(optionAll[j]);

                                    }
                                }
                                $('.ct').children().last().attr('question_id',dataAll[i].question_id);
                                break;
                            case 3:  //问答题
                                subjective();
                                $('.ct').children().last().find('.text').val(dataAll[i].question_body);
                                $('.ct').children().last().attr('question_id',dataAll[i].question_id);
                                break;
                            default:

                        }
                    }
                }else{
                    bootbox.alert(d.msg)
                }
            })



        }




        //判断题
        function judgement() {
            question_count+=1;
            var html='';
            html+=
                '<div class="judgement_mian main"  style="margin-top: 10px;" question_id="0" question_type="1" del_flag="0">'+
                "<div class='row'>" +
                "<div class='col-md-1 col-md-offset-11'>" +
                "<div style='width:26px;height:26px;float:right;'class='deleteDiv'></div>"+
                "</div>"+
                "</div>"+
                ' <div class="row" style="margin-bottom: 20px;">'+
                '<div class="col-md-3" style="text-align: right;padding-top: 5px;">'+
                '<span>'+
                '(判断题)<span style="color: red;">&nbsp;*</span><span class="question_count">'+question_count+'</span>.'+
                '</span>'+
                '</div>'+
                '<div class="col-md-6">'+
                '<input type="text" class="text" style="height: 34px;width:100%; border: 1px solid grey;" placeholder="这是个判断题...."/>'+
                '</div>'+
                '</div>'+
                '</div>';
            $(".ct").append(html);
        }
        //单选题
        function single() {
            question_count+=1;
            var html='';
            html+=
                '<div class="single_main main"  style="" question_id="0" question_type="0" del_flag="0">'+

                "<div class='row'>" +
                "<div class='col-md-1 col-md-offset-11'>" +
                "<div style='width:26px;height:26px;float:right;'class='deleteDiv'></div>"+
                "</div>"+
                "</div>"+
                ' <div class="row" style="margin-bottom: 20px;">'+
                '<div class="col-md-3" style="text-align: right;padding-top: 5px;">'+
                '<span>'+
                '(单选题)<span style="color: red;">&nbsp;*</span><span class="question_count">'+question_count+'</span>.'+
                '</span>'+
                '</div>'+
                '<div class="col-md-6">'+
                '<input type="text" class="text" style="height: 34px;width:100%; border: 1px solid grey;" placeholder="标题标题"/>'+
                '</div>'+
                '</div>'+
                '<div class="list1">'+

                '<div class="row" style="margin-bottom: 20px;">'+
                '<div class="col-md-3" style="text-align: right;padding-top: 5px;">'+
                '<span>'+
                '选项<span id="span1">1</span>.'+
                '</span>'+
                '</div>'+
                '<div class="col-md-6">'+
                '<input type="text" class="option" style="height: 34px;width:100%; border: 1px solid grey;" />'+
                '</div>'+
                '</div>'+

                '<div class="row" style="margin-bottom: 20px;">'+
                '<div class="col-md-3" style="text-align: right;padding-top: 5px;">'+
                '<span>'+
                '选项<span id="span1">2</span>.'+
                '</span>'+
                '</div>'+
                '<div class="col-md-6">'+
                '<input type="text" class="option" style="height: 34px;width:100%; border: 1px solid grey;" />'+
                '</div>'+
                '</div>'+

                '</div>'+

                '<div class="add">' +
                '<div class="row">' +
                '<div class="col-md-offset-9">' +
                '<a class="add1" href="#" style="color: #6ab7ad;padding-left: 10px;">'+
                '+添加多选'+
                '</a>'+
                '</div>'
            '</div>'
            '</div>'+

            '</div>';
            $(".ct").append(html);
        }
        //多选题
        function multiple() {
            question_count+=1;
            var html='';
            html+=
                '<div class="multiple_main main"  style="" question_id="0" question_type="2" del_flag="0">'+

                "<div class='row'>" +
                "<div class='col-md-1 col-md-offset-11'>" +
                "<div style='width:26px;height:26px;float:right;'class='deleteDiv'></div>"+
                "</div>"+
                "</div>"+
                ' <div class="row" style="margin-bottom: 20px;">'+
                '<div class="col-md-3" style="text-align: right;padding-top: 5px;">'+
                '<span>'+
                '(多选题)<span style="color: red;">&nbsp;*</span><span class="question_count">'+question_count+'</span>.'+
                '</span>'+
                '</div>'+
                '<div class="col-md-6">'+
                '<input type="text" class="text" style="height: 34px;width:100%; border: 1px solid grey;" placeholder="标题标题"/>'+
                '</div>'+
                '</div>'+
                '<div class="list2">'+

                '<div class="row" style="margin-bottom: 20px;">'+
                '<div class="col-md-3" style="text-align: right;padding-top: 5px;">'+
                '<span>'+
                '选项<span id="span1">1</span>.'+
                '</span>'+
                '</div>'+
                '<div class="col-md-6">'+
                '<input type="text" class="option" style="height: 34px;width:100%; border: 1px solid grey;" />'+
                '</div>'+
                '</div>'+

                '<div class="row" style="margin-bottom: 20px;">'+
                '<div class="col-md-3" style="text-align: right;padding-top: 5px;">'+
                '<span>'+
                '选项<span id="span1">2</span>.'+
                '</span>'+
                '</div>'+
                '<div class="col-md-6">'+
                '<input type="text" class="option" style="height: 34px;width:100%; border: 1px solid grey;"/>'+
                '</div>'+
                '</div>'+

                '</div>'+

                '<div class="add">' +
                '<div class="row">' +
                '<div class="col-md-offset-9">' +
                '<a class="add2" href="#" style="color: #6ab7ad;padding-left: 10px;">'+
                '+添加多选'+
                '</a>'+
                '</div>'
            '</div>'
            '</div>'+

            '</div>';
            $(".ct").append(html);
        }
        //问答题
        function subjective() {
            question_count+=1;
            var html='';
            html+='<div class="subjective_main main"  style="" question_id="0" question_type="3" del_flag="0">'+
                "<div class='row'>" +
                "<div class='col-md-1 col-md-offset-11'>" +
                "<div style='width:26px;height:26px;float:right;'class='deleteDiv'></div>"+
                "</div>"+
                "</div>"+
                '<div class="row" style="margin-bottom: 20px;">'+
                '<div class="col-md-3" style="text-align: right;padding-top: 5px;">'+
                '<span>'+
                '(问答题)<span style="color: red;">&nbsp;*</span><span class="question_count">'+question_count+'</span>.'+
                '</span>'+
                '</div>'+
                '<div class="col-md-6">'+
                '<textarea cols="5" class="text" style="width:100%; border: 1px solid grey;height: 150px;" placeholder="这个是问答题...."></textarea>'+
                '</div>'+
                '</div>'+
                '</div>';
            $(".ct").append(html);
        }

    })
});

